using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.Windows.Forms;

using Janus.Windows.GridEX;
namespace Janus.NorthwindDemo2007
{
	public partial class EmployeeDataControl
	{

		private bool mAddressVisible;

		protected override void OnDataSourceChanged()
		{
			if (DataSource != null)
			{
				this.editReportsTo.DataSource = NorthwindApp.NorthwindApplication.GetEmployeeList();
			}
			this.AddBinding(this.editEmployeeID, "Text", "Employees.EmployeeID");
			this.AddBinding(this.editTitleOfCourtesy, "SelectedValue", "Employees.TitleOfCourtesy");
			this.AddBinding(this.editFirstName, "Text", "Employees.FirstName");
			this.AddBinding(this.editLastName, "Text", "Employees.LastName");
			this.AddBinding(this.editJobTitle, "Text", "Employees.Title");
			this.AddBinding(this.editExtension, "Text", "Employees.Extension");
			this.AddBinding(this.editHireDate, "BindableValue", "Employees.HireDate");
			this.AddBinding(this.editNotes, "Text", "Employees.Notes");
			this.AddBinding(this.editReportsTo, "Value", "Employees.ReportsTo");
			this.AddBinding(this.editAddress, "Text", "Employees.Address");
			this.AddBinding(this.editCity, "Text", "Employees.City");
			this.AddBinding(this.editCountry, "Text", "Employees.Country");
			this.AddBinding(this.editPhone, "Text", "Employees.HomePhone");
			this.AddBinding(this.editPostalCode, "Text", "Employees.PostalCode");
			this.AddBinding(this.editRegion, "Text", "Employees.Region");

			if (this.DataSource != null)
			{
				this.gridPhoto.SetDataBinding(this.DataSource, "Employees");
			}

		}

		protected override void OnAllowEditChanged()
		{
			this.editTitleOfCourtesy.ReadOnly = ! this.AllowEdit;

			this.editFirstName.ReadOnly = ! this.AllowEdit;
			this.editLastName.ReadOnly = ! this.AllowEdit;
			this.editJobTitle.ReadOnly = ! this.AllowEdit;
			this.editExtension.ReadOnly = ! this.AllowEdit;
			this.editHireDate.ReadOnly = ! this.AllowEdit;
			this.editNotes.ReadOnly = ! this.AllowEdit;
			this.editReportsTo.ReadOnly = ! this.AllowEdit;
			this.btnSelectPhoto.Enabled = this.AllowEdit;
			this.btnClearPhoto.Enabled = this.AllowEdit;

		}
		public bool AddressVisible
		{
			get
			{
				return mAddressVisible;
			}
			set
			{
				if (mAddressVisible != value)
				{
					mAddressVisible = value;
					this.gbAddress.Visible = mAddressVisible;
				}
			}
		}
		public override System.Drawing.Size FullSize
		{
			get
			{
				Size sz = new Size(592, 384);
				if (! mAddressVisible)
				{
					sz.Height = sz.Height - this.gbAddress.Height;
				}
				return sz;
			}
		}

		private void editReportsTo_ValueChanged(object sender, System.EventArgs e)
		{

		}

		private void editReportsTo_Enter(object sender, System.EventArgs e)
		{
			this.editReportsTo.DroppedDown = true;
		}

		private void btnSelectPhoto_Click(object sender, System.EventArgs e)
		{

            if (this.IsAddNew)
            {
                try
                {
                    //If the current row is new, first try to update the current row.
                    CurrencyManager manager = (CurrencyManager)BindingContext[this.DataSource, "Employees"];
                    manager.EndCurrentEdit();
                }
                catch(Exception exc)
                {
                    MessageBox.Show(exc.Message,NorthwindApp.MessageCaption,MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
                    return;
                }
            }
			if (this.openPhotoDialog.ShowDialog(this) == DialogResult.OK)
			{
				try
				{
					Image img = Image.FromFile(openPhotoDialog.FileName);
					if (img != null)
					{
						this.gridPhoto.SetValue("Photo", img);
						this.gridPhoto.UpdateData();
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show("Invalid Image File", NorthwindApp.MessageCaption, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
			}
		}

		private void btnClearPhoto_Click(object sender, System.EventArgs e)
		{
            if (this.gridPhoto.CurrentRow != null)
            {
                this.gridPhoto.SetValue("Photo", DBNull.Value);
                this.gridPhoto.UpdateData();
            }
		}


	}

} //end of root namespace